package com.rbac.web.model.req;

import com.rbac.web.model.User;
import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.Optional;
import java.util.UUID;

/**
 * @author gengaoliu
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "用户添加请求参数")
public class UserAddReq {
  @Schema(description = "名称")
  @NotBlank(message = "名称不可为空")
  private String name;

  @Schema(description = "电话")
  @NotBlank(message = "电话不可为空")
  private String phone;

  @Schema(description = "账号")
  @NotBlank(message = "账号不可为空")
  private String username;

  @Schema(description = "密码")
  @NotBlank(message = "密码不可为空")
  private String password;

  public User toDomain() {
    return User.builder()
        .name(Optional.ofNullable(this.name).orElse(UUID.randomUUID().toString().substring(2, 10)))
        .phone(this.phone)
        .username(this.username)
        .password(this.password)
        .build();
  }
}
